package main;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import comunicacao.DataWatcherRunnable;
import comunicacao.GestorZooKeeper;
import comunicacao.RmiClient;
import entities.Server;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
@ManagedBean
@SessionScoped
public class Principal {

    private String name = "ola";
    private int clientID;
    private int currentAppServer = 0;
    private ArrayList<Server> appServers;
    private GestorZooKeeper gestorZK;

    /**
     * Creates a new instance of Principal
     */
    public Principal() {

    }

    private String start() {
        RmiClient rc = new RmiClient();
        return rc.start();
    }

    @PostConstruct
    public void carregou() {
        gerarID();
        gestorZK = new GestorZooKeeper();
        appServers = gestorZK.getAppServers();

        DataWatcherRunnable dw = new DataWatcherRunnable(null, this);
        dw.run();

    }

    public void gerarID() {
        Random rand = new Random();
        clientID = rand.nextInt(100000);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getClientID() {
        return clientID;
    }

    public void setClientID(int clientID) {
        this.clientID = clientID;
    }

    public int getCurrentAppServer() {
        return currentAppServer;
    }

    public synchronized void setCurrentAppServer(int currentAppServer) {
        this.currentAppServer = currentAppServer;
    }
    
    public synchronized void incrementCurrentAppServer(){
        
        int count = appServers.size();
        currentAppServer++;
        
        if (currentAppServer >= count){
            currentAppServer = 0;
        }
        
    }

    public ArrayList<Server> getAppServers() {
        return appServers;
    }

    public void setAppServers(ArrayList<Server> appServers) {
        this.appServers = appServers;
    }

    public GestorZooKeeper getGestorZK() {
        return gestorZK;
    }

    public void setGestorZK(GestorZooKeeper gestorZK) {
        this.gestorZK = gestorZK;
    }
    
    

}
